package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDao;
import com.tianqu.tms.dto.TransferProcessHistoryDto;

/**
 * 待办操作历史表
 * 
 * @author tianshifu
 * 
 */
public class TransferProcessHistory extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8312702692663017499L;

	private int id; // Id
	private String serial; // 车辆编号
	private int businessType;// 业务类型
	private String Reason;// 原因
	private int insertUserId;// 业务受理人
	private Timestamp insertTime;// 业务受理时间

	private int cssFlag;// 1,2区别（列表样式灰白区分）
	private int num;// 列表的序号
	private String businessTypeS;// 业务类型
	private String insertUserName;// 受理人
	private String insertTimeS;// 受理时间

	/**
	 * 
	 */

	public static TransferProcessHistory getInstance() {
		TransferProcessHistory his = new TransferProcessHistory();
		his.setExisting(false);
		return his;
	}

	/**
	 * 评估插入一条历史记录
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public void insertHisInfoAtAssess(TransferProcessHistory entity) throws TmsDaoException {
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		TransferProcessHistoryDto dto = new TransferProcessHistoryDto();
		dto = setDtoFromEntity(dto, entity);
		uDao.insertHisInfoAtAssess(dto);
	}

	/**
	 * 根据serial 载入历史记录
	 * 
	 * @param serial
	 * @return
	 * @throws TmsDaoException
	 */
	@SuppressWarnings("unchecked")
	public List loadtHistory(String serial) throws TmsDaoException {
		List transferProcessHistoryList = new ArrayList();
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao
				.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		List<TransferProcessHistoryDto> tList = new ArrayList<TransferProcessHistoryDto>();
		tList = uDao.loadTransferProcessHistoryBySerial(serial);
		int i = 1;
		if (tList != null && tList.size() > 0) {
			for (TransferProcessHistoryDto dto : tList) {
				TransferProcessHistory entity = new TransferProcessHistory();
				Codes code = Codes.getInstance();

				entity.setBusinessTypeS(code.getItemValueByItemIdAndItemCode(Codes.HIS_STATUS, dto.getBusinessType()));

				User user = User.getInstance();
				user = user.getUserInfoById(dto.getInsertUserId());
				entity.setInsertUserName(user.getRealName());
				entity.setNum(i);

				entity.setInsertTimeS(DateUtil.getTimeByTimeStamp1(dto.getInsertTime()));
				if (i % 2 != 0) {
					entity.setCssFlag(1);
				} else {
					entity.setCssFlag(2);
				}
				i++;
				transferProcessHistoryList.add(entity);
			}
		}
		return transferProcessHistoryList;
	}

	/**
	 * entity到dto
	 * 
	 * @return
	 */
	private TransferProcessHistoryDto setDtoFromEntity(TransferProcessHistoryDto dto, TransferProcessHistory entity) {
		dto.setSerial(entity.getSerial());
		dto.setBusinessType(entity.getBusinessType());
		dto.setInsertUserId(entity.getInsertUserId());
		dto.setReason(entity.getReason());

		return dto;
	}

	/**
	 * 根据流水号载出历史记录
	 * 
	 * @param serial
	 * @return
	 * @throws TmsDaoException
	 */
	public List loadtHistoryBySerial(String serial) throws TmsDaoException {
		List list = new ArrayList();
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao
				.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		List<TransferProcessHistoryDto> tList = new ArrayList<TransferProcessHistoryDto>();
		tList = uDao.loadTransferProcessHistoryBySerial(serial);
		int i = 1;
		if (tList != null && tList.size() > 0) {
			for (TransferProcessHistoryDto dto : tList) {
				TransferProcessHistory entity = new TransferProcessHistory();
				Codes code = Codes.getInstance();
				entity.setSerial(dto.getSerial());
				entity.setBusinessTypeS(code.getItemValueByItemIdAndItemCode(Codes.HIS_STATUS, dto.getBusinessType()));
				entity.setBusinessType(dto.getBusinessType());
				User user = User.getInstance();
				user = user.getUserInfoById(dto.getInsertUserId());
				if (user != null)
					entity.setInsertUserName(user.getRealName());
				entity.setNum(i);
				entity.setReason(dto.getReason());
				entity.setInsertTimeS(DateUtil.getTimeByTimeStamp1(dto.getInsertTime()));
				if (i % 2 != 0) {
					entity.setCssFlag(1);
				} else {
					entity.setCssFlag(2);
				}
				i++;
				list.add(entity);
			}
		}
		return list;
	}

	/**
	 * 根据流水号载出历史记录
	 * 
	 * @param serial
	 * @return
	 * @throws TmsDaoException
	 */
	public List searchtHistoryBySerial(String serial) throws TmsDaoException {
		List list = new ArrayList();
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao
				.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		List<TransferProcessHistoryDto> tList = new ArrayList<TransferProcessHistoryDto>();
		tList = uDao.loadTransferProcessHistoryBySerial(serial);
		int i = 1;
		if (tList != null && tList.size() > 0) {
			for (TransferProcessHistoryDto dto : tList) {
				TransferProcessHistory entity = new TransferProcessHistory();
				Codes code = Codes.getInstance();
				entity.setBusinessTypeS(
						code.getItemValueByItemIdAndItemCode(Codes.HIS_STATUS, dto.getBusinessType()));
				entity.setBusinessType(dto.getBusinessType());
				User user = User.getInstance();
				user = user.getUserInfoById(dto.getInsertUserId());
				entity.setInsertUserName(user.getRealName());
				entity.setNum(i);
				entity.setReason(dto.getReason());
				entity.setInsertTimeS(DateUtil.getTimeByTimeStamp1(dto.getInsertTime()));
				if (i % 2 != 0) {
					entity.setCssFlag(1);
				} else {
					entity.setCssFlag(2);
				}
				i++;
				list.add(entity);
			}
		}
		return list;
	}

	public void deleteHisInfoBySerial(String serial) throws TmsDaoException {
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao
				.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		uDao.deleteHisInfoBySerial(serial);
	}

	/**
	 * 根据流水号和类型查询历史记录
	 * 
	 * @param serial
	 * @param businessType
	 * @return
	 * @throws TmsDaoException
	 */
	public TransferProcessHistory findCheckFile(String serial, int businessType) throws TmsDaoException {
		TransferProcessHistoryDto dto = new TransferProcessHistoryDto();
		TransferProcessHistory entity = new TransferProcessHistory();
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao
				.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		dto = uDao.findCheckFile(serial, businessType);
		if (dto != null) {
			if (!StringUtil.isNullOrBlank(dto.getSerial())) {
				entity.setInsertTime(dto.getInsertTime());
				entity.setSerial(dto.getSerial());
			}
		}
		return entity;
	}

	public void insertHisInfoBySerial(TransferProcessHistoryDto dto) throws TmsDaoException {
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		uDao.insertHisInfoBySerial(dto);
	}
	
	/**
	 * 根据流水号载出历史记录
	 * 
	 * @param serial
	 * @return
	 * @throws TmsDaoException
	 */
	public List loadtHistoryBySerialInsertTime(String serial, String insertTime) throws TmsDaoException {
		List list = new ArrayList();
		TransferProcessHistoryDao uDao = (TransferProcessHistoryDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
		List<TransferProcessHistoryDto> tList = new ArrayList<TransferProcessHistoryDto>();
		tList = uDao.loadTransferProcessHistoryBySerialInsertTime(serial, insertTime);
		int i = 1;
		if (tList != null && tList.size() > 0) {
			for (TransferProcessHistoryDto dto : tList) {
				TransferProcessHistory entity = new TransferProcessHistory();
				Codes code = Codes.getInstance();
				entity.setSerial(dto.getSerial());
				entity.setBusinessTypeS(code.getItemValueByItemIdAndItemCode(Codes.HIS_STATUS, dto.getBusinessType()));
				entity.setBusinessType(dto.getBusinessType());
				User user = User.getInstance();
				user = user.getUserInfoById(dto.getInsertUserId());
				if (user != null)
					entity.setInsertUserName(user.getRealName());
				entity.setNum(i);
				entity.setReason(dto.getReason());
				entity.setInsertTimeS(DateUtil.getTimeByTimeStamp1(dto.getInsertTime()));
				if (i % 2 != 0) {
					entity.setCssFlag(1);
				} else {
					entity.setCssFlag(2);
				}
				i++;
				list.add(entity);
			}
		}
		return list;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public int getBusinessType() {
		return businessType;
	}

	public void setBusinessType(int businessType) {
		this.businessType = businessType;
	}

	public String getReason() {
		return Reason;
	}

	public void setReason(String reason) {
		Reason = reason;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public Timestamp getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Timestamp insertTime) {
		this.insertTime = insertTime;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getBusinessTypeS() {
		return businessTypeS;
	}

	public void setBusinessTypeS(String businessTypeS) {
		this.businessTypeS = businessTypeS;
	}

	public String getInsertUserName() {
		return insertUserName;
	}

	public void setInsertUserName(String insertUserName) {
		this.insertUserName = insertUserName;
	}

	public String getInsertTimeS() {
		return insertTimeS;
	}

	public void setInsertTimeS(String insertTimeS) {
		this.insertTimeS = insertTimeS;
	}

}
